package com.ruoyi.project.system.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SMSUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.Record;
import com.ruoyi.project.system.domain.SysBlacklist;
import com.ruoyi.project.system.domain.SysQuery;
import com.ruoyi.project.system.mapper.SysBlacklistMapper;
import com.ruoyi.project.system.mapper.SysQueryMapper;
import com.ruoyi.project.system.service.ISysQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 进件记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@Slf4j
@Service
public class SysQueryServiceImpl implements ISysQueryService {

    @Autowired
    private SysQueryMapper sysQueryMapper;

    @Autowired
    private SysBlacklistMapper sysBlacklistMapper;

    @Override
    public AjaxResult judicialQuery(SysQuery sysQuery) {
        AjaxResult result = new AjaxResult();
        JSONObject params = new JSONObject();
        params.put("req_time", System.currentTimeMillis());
        params.put("request_sn", "RYC" + IdUtils.fastSimpleUUID().substring(0, 13).toUpperCase() + "JF025");
        params.put("inter_id", "JF025");
        params.put("name", sysQuery.getName());
        params.put("cardNo", sysQuery.getCardNo());
        result.put("grss", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        return result;
    }

    @Override
    public AjaxResult bigDataQuery(SysQuery sysQuery) {
        AjaxResult result = new AjaxResult();
        JSONObject params = new JSONObject();
        params.put("req_time", System.currentTimeMillis());
        params.put("request_sn", "RYC" + IdUtils.fastSimpleUUID().substring(0, 13).toUpperCase() + "JF011");
        params.put("inter_id", "JF011");
        params.put("name", sysQuery.getName());
        params.put("cardNo", sysQuery.getCardNo());
        params.put("mobile", sysQuery.getMobile());
        result.put("dttz", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        return result;
    }

    @Override
    public AjaxResult bigDataQueryPro(SysQuery sysQuery) {
        AjaxResult result = new AjaxResult();
        JSONObject params = new JSONObject();
        String baseReqSn = "RYC" + IdUtils.fastSimpleUUID().substring(0, 13).toUpperCase();
        params.put("req_time", System.currentTimeMillis());
        // 个人诉讼
        params.put("request_sn", baseReqSn + "JF025");
        params.put("inter_id", "JF025");
        params.put("name", sysQuery.getName());
        params.put("cardNo", sysQuery.getCardNo());
        result.put("grss", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        // 手机三要素
        params.replace("request_sn", baseReqSn + "JF007");
        params.replace("inter_id", "JF007");
        params.put("mobile", sysQuery.getMobile());
        result.put("sjsys", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        // 多头
        params.replace("request_sn", baseReqSn + "JF011");
        params.replace("inter_id", "JF011");
        result.put("dttz", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        // 借选指数
        params.replace("request_sn", baseReqSn + "JF010");
        params.replace("inter_id", "JF010");
        result.put("jxzs", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        // 特殊名单验证
        String[] labelsList = sysBlacklistMapper.selectLabelsList(new SysBlacklist(sysQuery));
        JSONArray includes = new JSONArray();
        for (String labels : labelsList) {
            JSONArray array = JSON.parseArray(labels);
            for (int i = 0; i < array.size(); i++) {
                String label = array.getString(i);
                if (!includes.contains(label)) {
                    includes.add(label);
                }
            }
        }
        JSONObject labels = new JSONObject();
        labels.put("retcode", "000000");
        labels.put("retdata", includes.toJSONString());
        result.put("labels", labels);
        return result;
    }

    @Override
    public JSONObject companyQuery(SysQuery sysQuery) {
        JSONObject params = new JSONObject();
        String baseReqSn = "RYC" + IdUtils.fastSimpleUUID().substring(0, 13).toUpperCase();
        params.put("req_time", System.currentTimeMillis());
        // 企业纳税
        params.put("request_sn", baseReqSn + "JF033");
        params.put("inter_id", "JF033");
        params.put("mobile", sysQuery.getMobile());
        return JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params));
    }

    @Override
    public AjaxResult marryQuery(SysQuery sysQuery, String orderNo) {
        AjaxResult result = new AjaxResult();
        JSONObject params = new JSONObject();
        String baseReqSn = "RYC" + IdUtils.fastSimpleUUID().substring(0, 13).toUpperCase();
        params.put("req_time", System.currentTimeMillis());
        // 实名认证
        params.put("request_sn", baseReqSn + "JF005");
        params.put("inter_id", "JF005");
        params.put("name", sysQuery.getName());
        params.put("cardNo", sysQuery.getCardNo());
        result.put("smrz", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        // 个人诉讼
        params.replace("request_sn", baseReqSn + "JF025");
        params.replace("inter_id", "JF025");
        result.put("grss", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));
        // 婚姻
//        params.put("mobile", sysQuery.getMobile());
        params.replace("request_sn", baseReqSn + "JF032");
        params.replace("inter_id", "JF032");
        result.put("hyhy", JSON.parseObject(HttpUtils.sendPost("http://175.178.170.139:8089/jiekou/jufaAndi", params)));

        SMSUtils.sendReport(sysQuery.getMobile(), "mar", orderNo);

        return result;
    }

    @Override
    public List<Record> getRecords(String mobile, String sign) {
        List<Record> recordList = sysQueryMapper.recordList(mobile, sign);
        // 处理数据
        for (Record record : recordList) {
            if (record.getProductId() == 3) {         // 企业 MK
                String m = record.getMobile();
                String mk = record.getMoreKeyword();
                record.setMoreKeyword(StringUtils.isEmpty(mk) ? m : mk);
            } else {
                record.setMoreKeyword(StringUtils.isEmpty(record.getName()) ? "查询关键字不全" : ("**" + record.getName().substring(1)));
            }
            record.setPayState("SUCCESS".equals(record.getPayState()) ? "-查询完成" : "");
            record.setName(null);
            record.setCardNo(null);
            record.setMobile(null);
        }
        return recordList;
    }

    /**
     * 查询进件记录
     *
     * @param id 进件记录主键
     * @return 进件记录
     */
    @Override
    public SysQuery selectSysQueryById(Long id) {
        return sysQueryMapper.selectSysQueryById(id);
    }

    /**
     * 查询进件记录列表
     *
     * @param sysQuery 进件记录
     * @return 进件记录
     */
    @Override
    public List<SysQuery> selectSysQueryList(SysQuery sysQuery) {
        return sysQueryMapper.selectSysQueryList(sysQuery);
    }

    /**
     * 新增进件记录
     *
     * @param sysQuery 进件记录
     * @return 结果
     */
    @Override
    public int insertSysQuery(SysQuery sysQuery) {
        return sysQueryMapper.insertSysQuery(sysQuery);
    }

    /**
     * 修改进件记录
     *
     * @param sysQuery 进件记录
     * @return 结果
     */
    @Override
    public int updateSysQuery(SysQuery sysQuery) {
        return sysQueryMapper.updateSysQuery(sysQuery);
    }

}
